package com.zhouxiaoge.ST;

import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.STGroupFile;
import org.stringtemplate.v4.STGroupString;

import java.util.LinkedList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        // 单行模板
        String template = "sqlTemplate(columns,condition) ::= \"select <columns> from table where 1=1 <if(condition)>and <condition><endif>\"";
        // 多行模板
        String template1 = "sqlTemplate(columns,condition) ::= <<select <columns> \n" + "from table \n" + "where 1=1 <if(condition)>and <condition><endif> >>";

        // 多行模板 忽略换行、缩进
        String template2 = "sqlTemplate(columns,condition) ::= <% select <columns> \n" + "from table \n" + "where 1=1 <if(condition)>and <condition><endif> %>";

        STGroup stGroup = new STGroupString(template);
        ST sqlTemplate = stGroup.getInstanceOf("sqlTemplate");
        sqlTemplate.add("columns", "order_id");
        sqlTemplate.add("condition", "dt=20220708");
        String render = sqlTemplate.render();
        System.out.println(render);
        System.out.println("===============================================");
        STGroupFile stGroupFile = new STGroupFile("simple.stg");
        ST sqlTemplate1 = stGroupFile.getInstanceOf("sqlTemplate");
        List<String> columnList = new LinkedList<>();
        columnList.add("id");
        columnList.add("name");
        columnList.add("age");
        sqlTemplate1.add("columns", columnList);
        sqlTemplate1.add("condition", "dt=2022-07-08");
        String render1 = sqlTemplate1.render();
        System.out.println(render1);
        System.out.println("===============================================");
        STGroup stg = new STGroupFile("complex.stg");
        ST sqlST = stg.getInstanceOf("sqlTemplate");

        List<String> complexColumnList = new LinkedList<>();
        complexColumnList.add("order_id");
        complexColumnList.add("price");
        complexColumnList.add("phone");
        complexColumnList.add("user");

        sqlST.add("columns", complexColumnList);
        sqlST.add("condition", "dt='2017-04-04'");
        sqlST.add("joinKey", "user");
        sqlST.add("tableName", "orderTable");

        List<String> childColumnList = new LinkedList<String>();
        childColumnList.add("user");
        childColumnList.add("userLeave");
        childColumnList.add("userLocation");
        sqlST.add("childColumns", childColumnList);
        sqlST.add("childJoinKey", "user");
        sqlST.add("childTableName", "userTable");

        String result = sqlST.render();

        System.out.println(result);
        System.out.println("===============================================");
        ST st = new ST("hello, $if(name)$$name$$endif$", '$', '$');
        st.add("name", "zhouxiaoge");
        System.out.println(st.render());
        System.out.println("===============================================");
        String bracket = "say(name) ::= << hello, <name:bracket();separator=\",\"> >> " + "bracket(x) ::= << (<x>) >> ";
        String anonymityTemplate = "say(name) ::= << hello, <name:{x|[<x>]};separator=\",\"> >> ";
        STGroupString st1 = new STGroupString(anonymityTemplate);
        ST say = st1.getInstanceOf("say");
        say.add("name", "java");
        say.add("name", "php");
        String render2 = say.render();
        System.out.println(render2);

        ST st2 = new ST("<b>$u.id$</b>:$u.name$", '$', '$');
        st2.add("u", new User(1, "123"));
        System.out.println(st2.render());

        STGroupString stGroupString = new STGroupString("introduction(person) ::= \"I`m <person.name>, I`m <person.age> years old.\"");
        ST introduction = stGroupString.getInstanceOf("introduction");
        introduction.addAggr("person.{name,age}", "zhouxiaoge", 19);
        System.out.println(introduction.render());

    }


}

class User {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }
}
